package com.joesem.sale.client.logic.event

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentOrderContentBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.base.launch
import com.joesem.sale.client.logic.workOrder.WorkOrderFragment.Filter.about_me
import com.joesem.sale.client.network.data.EventListMode
import com.joesem.sale.client.utils.click


class EventContentListFragment : BaseFragment<FragmentOrderContentBinding>() {
    private val adapter =
        object : BaseQuickAdapter<EventListMode, BaseViewHolder>(R.layout.item_event) {
            var itemClick: (EventListMode) -> Unit = {}
            override fun convert(holder: BaseViewHolder, item: EventListMode) {
                holder.setText(R.id.tvTitle, item.type.orEmpty())
                holder.setText(R.id.tvStatus, item.status.orEmpty())
                holder.setText(R.id.tvCustomer, "客户：${item.customer?.name.orEmpty()}")
                holder.setText(R.id.tvOrderNo, "事件编号：${item.event_number}")
                holder.setText(R.id.tvCustomerAddress, "地址：${item.address}")
                holder.setText(R.id.tvPlanDate, "创建时间：${item.create_time_format}")
                holder.itemView.click {
                    itemClick.invoke(item)
                }
            }
        }
    private val viewModel by createViewModel<EventViewModel>()

    private var stateParams: String? = ""

    override fun FragmentOrderContentBinding.initView(savedInstanceState: Bundle?) {
        stateParams = arguments?.getString("state")
        rvContent.adapter = adapter
        srlRefresh.autoRefresh()
    }

    override fun FragmentOrderContentBinding.initListener() {
        srlRefresh.apply {
            setOnRefreshListener {
                viewModel.eventLis(state = stateParams, params = getParams(), isFirst = true)
            }
            setOnLoadMoreListener {
                viewModel.eventLis(state = stateParams, params = getParams(), isFirst = true)
            }
        }
        adapter.itemClick = {
            launch<EventDetailsActivity>("eventId" to it.id)
        }
    }

    override fun FragmentOrderContentBinding.initIntent(intent: Any?) {
        when (intent) {
            is EventViewModel.EventListIntent -> {
                srlRefresh.apply {
                    finishRefresh()
                    finishLoadMore()
                    if (intent.isFirst) {
                        adapter.setList(intent.data)
                        if (adapter.data.isEmpty()) {
                            adapter.setEmptyView(R.layout.layout_empty)
                        }
                    } else {
                        adapter.addData(intent.data ?: arrayListOf())
                    }
                }
            }
        }
    }

    private fun getParams() = (requireActivity() as EventListActivity).filterParams

    fun refresh() {
        if (this.isAdded && !this.isHidden)
            bind.srlRefresh.autoRefresh()
    }
}